package com.leadership.api.handler.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;
import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.http.annotation.Rao;
import com.demo2do.core.utils.JsonUtils;
import com.leadership.api.entity.SurveyResponseItem;
import com.leadership.api.handler.SurveyHandler;
import com.leadership.api.rao.SurveyRao;
import com.leadership.entity.survey.SurveyProject;
import com.leadership.entity.survey.SurveyResult;

/**
 * 
 * @author mohanlan
 *
 */
@Component("surveyHandler")
public class SurveyHandlerImpl implements SurveyHandler {
    
    //private static final Logger logger = LoggerFactory.getLogger(SurveyHandler.class);
    
    @Rao
    private SurveyRao surveyRao;
    
    /*
     * (non-Javadoc)
     * @see com.leadership.api.handler.SurveyHandler#syncSurveyItems()
     */
    public List<SurveyProject> syncSurveyItems() {
        HttpResult httpResult = surveyRao.listItems("http://pinggu.cifi.com.cn/api/items.list?page=0&size=100000");
        if(httpResult.isValid()) {
            
            String responseText = httpResult.getResponseText();
            Map<String, Object> response = JsonUtils.parse(responseText);
            if(response.get("content") != null) {
                String content = JsonUtils.toJson(response.get("content"));
                List<SurveyProject> surveyProjects = JsonUtils.parseAsList(content, SurveyProject.class);
                return surveyProjects;
            } else {
                throw BusinessException.newInstance("504", responseText);
            }
            
        }
        
        throw BusinessException.newInstance("500", "请求360系统发生异常");
    }
    
    /**
     * 
     */
    public List<SurveyResult> getSurveyStatus(Integer itemId, String templateType) {
        
        HttpResult httpResult = surveyRao.listItems("http://pinggu.cifi.com.cn/api/examination/progress?items="+ itemId);
        //logger.info("getSurveyStatus response [{}]", JsonUtils.toJson(httpResult));
        
        if(httpResult.isValid()) {
            
            String responseText = httpResult.getResponseText();
            List<SurveyResult> result = this.generateResult(templateType, responseText);
            
            return result;
        }
        throw BusinessException.newInstance("504", "请求360接口发生异常");
    }
    
    /**
     * 
     * @param templateType
     * @param response
     * @return
     */
    private List<SurveyResult> generateResult(String templateType, String response) {
        
        List<SurveyResult> results = new ArrayList<SurveyResult>();
        
        List<SurveyResponseItem> items = JsonUtils.parseAsList(response, SurveyResponseItem.class);
        for(SurveyResponseItem surveyResponseItem : items) {
            String status = surveyResponseItem.getWait().size() == 0 ? "finish" : "processing";
            SurveyResult surveyResult = new SurveyResult(templateType, surveyResponseItem.getUser().getEmail(), status, "http://pinggu.cifi.com.cn" + surveyResponseItem.getPowerPointUri());
            results.add(surveyResult);
        }
        
        return results;
    }

}
